package com.li.manage.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import com.li.commons.util.FileUtil;

public class BaseAction {

	@Value("${domain.name}")
	public String DOMAIN_NAME;
	
	@Value("${img.base.dir}")
	public String IMG_BASE_DIR;
	
	@Value("${img.user.header.dir}")
	public String IMG_USER_HEADER_DIR;
	
	public String uploadImg(String imgUrl, MultipartFile file)
			throws IOException {
		File distFile = new File(IMG_BASE_DIR, imgUrl);
		if (!distFile.getParentFile().exists()) {
			distFile.getParentFile().mkdirs();
		}
		if (!distFile.exists()) {
			distFile.createNewFile();
		}
		if (file != null && file.getSize() > 0) {
			FileUtil.copyFile(file.getInputStream(), new FileOutputStream(
					distFile));
		}
		return null;
	}
	public void deleteImg(String imgUrl) throws IOException {
		File distFile = new File(IMG_BASE_DIR+ imgUrl);
		if (distFile.exists()) {
			distFile.delete();
		}
	}
}
